<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\common\model\oms;

use think\Model;
use think\Log;

/**
 * Description of Base
 *
 * @author Expect
 */
class Base extends Model {

    protected $connection = ['database' => 'oms'];
    protected $newArr;

    /**
     * 将下划线命名转换为驼峰式命名 只支持二维
     * @author
     * @date
     * 
     */
    public function __construct($data = []) {
        parent::__construct($data);
    }

    function convertUnderline($arr, $status = true, $key = '', $ucfirst = false) {
        if (!is_array($arr)) {
            return false;
        }
        foreach ($arr as $k => $value) {
            $str = ucwords(str_replace('_', ' ', $k));
            $str = str_replace(' ', '', lcfirst($str));
            $str = $ucfirst ? ucfirst($str) : $str;
            if (!$status) {
                if ($key) {
                    $this->newArr[$key][$str] = (string) $value;
                }
            } else {
                $this->newArr[$str] = (string) $value;
            }
            if (is_array($value)) {
                unset($this->newArr[$str]);
                $this->convertUnderline($value, false, $str);
            }
        }
        return $this->newArr;
    }

    /**
     * 转换数据
     * @param [] $list
     * @return []
     */
    public function dataInit($list, $status = false) {
        if (empty($list)) {
            return (object) [];
        }
        foreach ($list as $vip) {
            $data['memberCode'] = empty($vip['member_code']) ? '' : (string) $vip['member_code'];
            $data['vipId'] = empty($vip['vip_id']) ? '' : (string) $vip['vip_id'];
            $data['comeFrom'] = empty($vip['come_from']) ? '' : (string) $vip['come_from'];
            $data['mpId'] = empty($vip['mp_id']) ? '' : (string) $vip['mp_id'];
            $data['accountCode'] = empty($vip['account_code']) ? '' : (string) $vip['account_code'];
            $data['name'] = empty($vip['name']) ? '' : (string) $vip['name'];
            $data['gender'] = empty($vip['gender']) ? '' : (string) $vip['gender'];
            $data['birthday'] = empty($vip['birthday']) ? '' : (string) $vip['birthday'];
            $data['mobile'] = empty($vip['mobile']) ? '' : (string) $vip['mobile'];
            $data['mobileStatus'] = empty($vip['mobile_status']) ? '' : (string) $vip['mobile_status'];
            $data['cardNo'] = empty($vip['cardno']) ? '' : (string) $vip['cardno'];
            $data['cardLev'] = empty($vip['card_lev']) ? '0' : (string) $vip['card_lev'];
            $data['headerImg'] = empty($vip['header_img']) ? '' : (string) $vip['header_img'];
            $data['addTs'] = empty($vip['add_ts']) ? '' : (string) date('Y-m-d', $vip['add_ts']);
            $data['lastTs'] = empty($vip['last_ts']) ? '' : (string) date('Y-m-d', $vip['last_ts']);
            $returnData[] = $data;
        }
        return $status ? $data : $returnData;
    }

    /**
     * 资料完整度打分
     * 手机/姓名/性别/生日    每项25分
     */
    protected function checkDataIntegrity($value) {

        Log::record('checkDataIntegrity->修改资料进来时候的value参数:' . var_export($value, true), 'INFO');
        $dataIntegrity = 0;
        if (!empty($value['name']) && $value['name'] != 'no') {
            $dataIntegrity += 25;
        }

        if (!empty($value['mobile']) && strlen($value['mobile']) == 11) {
            $dataIntegrity += 10;
        }
        if (!empty($value['mobileStatus']) && $value['mobileStatus'] == 1) {
            $dataIntegrity += 15;
        }
        if (!empty($value['gender']) && $value['gender'] != '0') {
            $dataIntegrity += 25;
        }
        if (!empty($value['birthday']) && $value['birthday'] != '0000-00-00') {
            $dataIntegrity += 25;
        }
        $value['data_integrity'] = $dataIntegrity; //总结得分
        return $value;
    }

}
